package com.me.testcustomview;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class SubjectCategory extends ImageView {
	private JSONObject data;
	private String subjectId;
	private String subjectName;
	private String imageName;
	private Context mContext;
	public boolean isCircleConner = true;

	public SubjectCategory(Context context) {
		super(context);
		this.mContext = context;
	}

	public SubjectCategory(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
	}

	public SubjectCategory(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
	}

	public JSONObject getData() {
		return data;
	}

	public void setData(JSONObject data) {
		this.data = data;
		
		try {
			setSubjectId(data.getString("id"));
			setSubjectName(data.getString("name"));
			setImageName(data.getString("image"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public String getSubjectId() {
		return subjectId;
	}

	public void setSubjectId(String subjectId) {
		this.subjectId = subjectId;
	}

	public String getSubjectName() {
		return subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}

	public String getImageName() {
		return imageName;
	}

	public void setImageName(String imageName) {
		if(!(imageName.equals("")||imageName == null)) {
			this.imageName = imageName;
			this.setImageResource(R.drawable.hoa);
			//Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.hoa);
		//this.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(imageName, "drawable", mContext.getPackageName())));
			//this.setImageBitmap(getCircleCornerBitmap(bitmap, false));
		}
	}
	
	 public Bitmap getCircleCornerBitmap(Bitmap bitmap, boolean isFitImageView) {
		 if (isFitImageView) bitmap = Bitmap.createScaledBitmap(bitmap, getWidth(), getHeight(), true);
		    Bitmap output; 
		    output = Bitmap.createBitmap(bitmap.getWidth(),
		        bitmap.getHeight(), Config.ARGB_8888);
		    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
		    Paint paint = new Paint();
		    paint.setShader(shader);
		    Canvas c = new Canvas(output);
		    c.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, paint);
		   return output; 
		  }
	 @Override
	protected void onDraw(Canvas canvas) {
		if (isCircleConner) {
			Bitmap b =  ((BitmapDrawable)getDrawable()).getBitmap() ;
	        canvas.drawBitmap(getCircleCornerBitmap(b, true), 0, 0, null);
	    }
		else super.onDraw(canvas);
	}
	 
	
	

}
